/*
 * @Author: Arthur
 * @Date: 2020-06-17 19:59:01
 * @LastEditors: Arthur
 * @LastEditTime: 2020-06-29 19:00:32
 * @Description: file content
 */
import axios from 'axios'
// import store from '../store/index.js'

// import {
//     Message,
//     MessageBox
// } from 'element-ui'
axios.defaults.baseURL = 'https://api.xiaozhucaishui.com'



axios.interceptors.request.use(request => {
    try {
        if(window && request.notoken !== 1){
            const token = window.localStorage.getItem('token')
            token && (request.headers.token = token)
        }
    } catch (error) {
        
    }

    return request;
  }, error => {
    return Promise.reject(error);
  });
axios.interceptors.response.use(
    (res) => {

        if (res.data.code == 0) {
            // if (res.config.url !== '/api/user/getUserActivity')
            //     Message({
            //         message: res.data.msg,
            //         type: 'warning'
            //     })
        }
        return res
    },
    function (error) {
        // alert('请求超时，请检查网络！')
        // store.dispatch('common/removeFromRequestQueue')
        // 对响应错误做点什么
        if (error && error.response && error.response.data && error.response.data.code == 401) {
            // store.commit('common/setloginShow', true)
            try {
                if (window) {
                    window.localStorage.removeItem('token')
                    window.localStorage.removeItem("userInfo");
                    window.location.href = "/login?url=" + window.location.pathname + window.location.search
                }
            } catch (error) {

            }
            // Message({
            //     message: error.response.data.msg,
            //     type: 'warning'
            // })
        }
        return Promise.reject(error)
    }
)
// 处理返回值
function resultHandle(res, resolve) {
    resolve(res)
}
export function get(url, params, config) {
    return new Promise((resolve, reject) => {
        axios({
            method: "get",
            url: url,
            params: params,
            headers: config,
            responseType: 'blob'
        }).then((res) => {
            resultHandle(res.data, resolve, reject)
        }, err => {
            reject(err)
        })
    })
}
export function post(url, params, config = {
    noLoading: false
}) {
    return new Promise((resolve, reject) => {
        axios.post(url, params, config).then(res => {

            resultHandle(res.data, resolve, reject)
        }, err => {
            reject(err)
        })
    })
}

export function formdataPost(url, params, config = {
    noLoading: false
}) {
    return new Promise((resolve, reject) => {
        let formdata = new FormData()
        formdata.append("file", params.file)

        axios.post(url, formdata, config).then(res => {

            resultHandle(res.data, resolve, reject)
        }, err => {
            reject(err)
        })
    })
}

export const APIURL = axios.defaults.baseURL